package com.zf.apartment.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zf.common.annotation.Log;
import com.zf.common.core.controller.BaseController;
import com.zf.common.core.domain.AjaxResult;
import com.zf.common.enums.BusinessType;
import com.zf.apartment.domain.Location;
import com.zf.apartment.service.ILocationService;
import com.zf.common.utils.poi.ExcelUtil;
import com.zf.common.core.page.TableDataInfo;

/**
 * 位置Controller
 * 
 * @author rxf
 * @date 2024-09-29
 */
@RestController
@RequestMapping("/apartment/location")
public class LocationController extends BaseController
{
    @Autowired
    private ILocationService locationService;

    /**
     * 查询位置列表
     */
    @PreAuthorize("@ss.hasPermi('apartment:location:list')")
    @GetMapping("/list")
    public TableDataInfo list(Location location)
    {
        startPage();
        List<Location> list = locationService.selectLocationList(location);
        return getDataTable(list);
    }
    @GetMapping("/provinces")
    public TableDataInfo getProvinces() {
        startPage();
        List<Location> list = locationService.selectProvinces();
        return getDataTable(list);
    }

    @GetMapping("/cities")
    public TableDataInfo getCities(@RequestParam Long provinceId) {
        startPage();
        List<Location> list = locationService.selectCitiesByProvince(provinceId);
        return getDataTable(list);
    }

    @GetMapping("/areas")
    public TableDataInfo getAreas(@RequestParam Long cityId) {
        startPage();
        List<Location> list = locationService.selectAreasByCity(cityId);
        return getDataTable(list);
    }
    /**
     * 导出位置列表
     */
    @PreAuthorize("@ss.hasPermi('apartment:location:export')")
    @Log(title = "位置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Location location)
    {
        List<Location> list = locationService.selectLocationList(location);
        ExcelUtil<Location> util = new ExcelUtil<Location>(Location.class);
        util.exportExcel(response, list, "位置数据");
    }

    /**
     * 获取位置详细信息
     */
    @PreAuthorize("@ss.hasPermi('apartment:location:query')")
    @GetMapping(value = "/{locationId}")
    public AjaxResult getInfo(@PathVariable("locationId") Long locationId)
    {
        return success(locationService.selectLocationByLocationId(locationId));
    }

    /**
     * 新增位置
     */
    @PreAuthorize("@ss.hasPermi('apartment:location:add')")
    @Log(title = "位置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Location location)
    {
        return toAjax(locationService.insertLocation(location));
    }

    /**
     * 修改位置
     */
    @PreAuthorize("@ss.hasPermi('apartment:location:edit')")
    @Log(title = "位置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Location location)
    {
        return toAjax(locationService.updateLocation(location));
    }

    /**
     * 删除位置
     */
    @PreAuthorize("@ss.hasPermi('apartment:location:remove')")
    @Log(title = "位置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{locationIds}")
    public AjaxResult remove(@PathVariable Long[] locationIds)
    {
        return toAjax(locationService.deleteLocationByLocationIds(locationIds));
    }
}
